home *** CD-ROM | disk | FTP | other *** search
/ MacGames Sampler / PHT MacGames Bundle.iso / MacSource Folder / Samples from the CD / Editors / emacs / Emacs-1.14b1-sources / sources / utility-src / ispell / freq.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-04  |  2.7 KB  |  119 lines  |  [TEXT/EMAC]

  1. /* Copyright (C) 1990, 1993 Free Software Foundation, Inc.
  2.  
  3.    This file is part of GNU ISPELL.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. /* compute the frequencies of character pairs in a file
  20.    then write a list of characters used, plus a sorted list
  21.    of frequencies
  22.  
  23.    this program needs to run on 80286's, so it doesn't use
  24.    any individual data structure larger than 64k. */
  25.  
  26. #include <stdio.h>
  27. #include <ctype.h>
  28. #include "charset.h"
  29.  
  30. /* this array contains letters to use when generating near misses */
  31. char near_miss_letters[256];
  32. int nnear_miss_letters;
  33.  
  34. /* this array has 1 for any character that is in near_miss_letters */
  35. char near_map[256];
  36.  
  37. long *table[256];
  38. char used[256];
  39.  
  40. int
  41. main ()
  42. {
  43.   int c1, c2;
  44.   long count;
  45.   int i, j;
  46.   FILE *out;
  47.   
  48. {
  49.     // ./freq < $(srcdir)/dict > freqtbl
  50.     FILE *fp;
  51. //    char *_argv[] = { "freq", 0L };
  52. //    argc = sizeof(_argv) / sizeof(char *) - 1;
  53. //    argv = _argv;
  54.     init_unix_io_driver(1,0L);
  55.     fp = freopen("./dict","r",stdin);
  56.     if (fp == 0L) exit(1);
  57.     fp = freopen("freqtbl","w",stdout);
  58.     if (fp == 0L) exit(1);
  59. }
  60.  
  61.   if ((out = popen ("sort -nr", "w")) == NULL)
  62.     {
  63.       fprintf (stderr, "can't make pipe to sort\n");
  64.       exit (1);
  65.     }
  66.  
  67.   for (i = 0; i < 256; i++)
  68.     table[i] = (long *) xcalloc (256, sizeof (long));
  69.  
  70.   c1 = 0;
  71.   while (c1 == 0)
  72.     {
  73.       c1 = getchar ();
  74.       if (c1 == EOF)
  75.     {
  76.       fprintf (stderr, "no input\n");
  77.       exit (1);
  78.     }
  79.       c1 = charset[c1].lowercase;
  80.     }
  81.   used[c1] = 1;
  82.  
  83.   while ((c2 = getchar ()) != EOF)
  84.     {
  85.       c2 = charset[c2].lowercase;
  86.       if (c2 == 0)
  87.     continue;
  88.       used[c2] = 1;
  89.       table[c1][c2]++;
  90.       c1 = c2;
  91.     }
  92.  
  93.   /* a big "count" so this line will be at the top of the
  94.      * sorted output
  95.      */
  96.   fprintf (out, "30000 ");
  97.   for (c1 = 0; c1 < 256; c1++)
  98.     if (used[c1])
  99.       putc (c1, out);
  100.   putc ('\n', out);
  101.  
  102.   for (i = 0; i < 256; i++)
  103.     {
  104.       if (used[i] == 0)
  105.     continue;
  106.       for (j = 0; j < 256; j++)
  107.     {
  108.       if (used[j] == 0)
  109.         continue;
  110.       count = table[i][j];
  111.       if (count)
  112.         fprintf (out, "%ld %c%c\n", count, i, j);
  113.     }
  114.     }
  115.  
  116.   pclose (out);
  117.   return 0;
  118. }
  119.